package jnpf.service.impl;

import cn.hutool.core.util.StrUtil;
import jnpf.base.UserInfo;
import jnpf.model.common.ZXGL_Leaders;
import jnpf.model.common.ZXGL_XzbmLeaders;
import jnpf.model.common.ZXGL_ZzbmLeaders;
import jnpf.permission.entity.OrganizeEntity;
import jnpf.permission.service.OrganizeService;
import jnpf.service.ZXGL_OrganizeService;
import jnpf.util.UserProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ZXGL_OrganizeServiceImpl implements ZXGL_OrganizeService {

    @Autowired
    private UserProvider userProvider;

    @Autowired
    private OrganizeService organizeService;

    /**
     * 登录人
     * @return
     */
    @Override
    public ZXGL_Leaders getLeaders() {
        ZXGL_Leaders leaders = new ZXGL_Leaders();
        UserInfo userInfo = userProvider.get();
        if (userInfo != null && StrUtil.isNotEmpty(userInfo.getOrganizeId())) {
            // OrganizeEntity entity = organizeApi.getById(userInfo.getOrganizeId());
            OrganizeEntity entity = organizeService.getById(userInfo.getDepartmentId());
            leaders.setDepartmentManager(entity.getDirector());
            leaders.setDepartmentHeadleader(entity.getManager());
        }
        return leaders;
    }

    /**
     * 主责部门
     * @param zzbm
     * @return
     */
    @Override
    public ZXGL_ZzbmLeaders getZzbmLeaders(String zzbm) {
        ZXGL_ZzbmLeaders leaders = new ZXGL_ZzbmLeaders();

        if (StrUtil.isNotEmpty(zzbm)) {
            OrganizeEntity entity = organizeService.getById(zzbm);
            leaders.setDepartmentManagerZzbm(entity.getDirector());
            leaders.setDepartmentHeadleaderZzbm(entity.getManager());
        }

        return leaders;
    }

    /**
     * 协助部门
     * @param xzbm
     * @return
     */
    @Override
    public ZXGL_XzbmLeaders getXzbmLeaders(String xzbm) {
        ZXGL_XzbmLeaders leaders = new ZXGL_XzbmLeaders();

        if (StrUtil.isNotEmpty(xzbm)) {
            String[] strArr = xzbm.split(",");
            // 协作部门负责人
            String departmentManagerXzbm = "";
            // 协作部门主管领导
            String departmentHeadleaderXzbm = "";
            for (String id : strArr) {
                if (StrUtil.isNotEmpty(id)) {
                    OrganizeEntity entity = organizeService.getById(id);
                    if (StrUtil.isNotEmpty(departmentManagerXzbm)) {
                        departmentManagerXzbm = entity.getDirector();
                    } else {
                        departmentManagerXzbm = departmentManagerXzbm + "," + entity.getDirector();
                    }

                    if (StrUtil.isNotEmpty(departmentHeadleaderXzbm)) {
                        departmentHeadleaderXzbm = entity.getDirector();
                    } else {
                        departmentHeadleaderXzbm = departmentManagerXzbm + "," + entity.getManager();
                    }
                }
            }
            leaders.setDepartmentManagerXzbm(departmentManagerXzbm);
            leaders.setDepartmentHeadleaderXzbm(departmentHeadleaderXzbm);
        }
        return leaders;
    }
}
